/*
 * TCSS 422 Web Spider Project
 * Group Money: Al McKenzie, Michael Pitts, Taylor Zielske
 */
package control.threads;

/**
 * This thread continues until its setGo() method is feed a false value, at which point
 * it will finish what it is doing and then stop.
 * 
 * @author Michael Pitts
 * @version Oct 31, 2011
 */
public class ProcessThread extends Thread {
	
	/**
	 * The runnable that this thread will do while my_go == true.
	 */
	private final Runnable my_runnable;
	
	/**
	 * While true, causes the thread to run.
	 */
	private boolean my_go;
	
	/**
	 * Creates a new thread that can process urls, using the given runnable.
	 * @param the_runnable is what the thread should do while my_go is true.
	 */
	public ProcessThread(final Runnable the_runnable) {
		super();
		my_runnable = the_runnable;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override public void run() {
		setGo(true);
		while (getGo()) { // thread safe check if called to stop.
			my_runnable.run();
		}
	}
	
	/**
	 * Sets my go value in a thread safe manner.
	 * @param the_go_value is the new value of go.
	 */
	public final synchronized void setGo(final boolean the_go_value) {
		my_go = the_go_value;
	}
	
	/**
	 * Gets the current value of go in a thread safe manner.
	 * @return the current value of go.
	 */
	private synchronized boolean getGo() {
		return my_go;
	}

}
